<?php
namespace Admin\Controller;
//use Think\Controller;
class CategoryController extends CommontController {
	//系别分类添加
	public function add(){
		//系别分类显示
        $categoryModel=D('Category');
        $data=$categoryModel->select();
        $data=getCatTree($data);
        $this->assign('data',$data);
        //系别分类添加实现
        if(IS_POST){
        	if($categoryModel->create()){
        		if($categoryModel->add()){
        			$this->success("添加成功",U("Category/index"));die;
        		}else{
        			$this->error("添加失败");
        		}
        	}else{
        		$this->error($categoryModel->getError());
        	}
        }
        //渲染模板
        $this->display();
    }
    //系别分类列表
    public function index(){
    	$categoryModel=D('Category');
    	$list=$categoryModel->select();
    	$list=getCatTree($list);
    	$this->assign('list',$list);
    	$this->display();
    }
    //系别分类修改
    public function upd(){
    	//实例化类模型
    	$categoryModel=D('Category');
    	//系别分类显示
        $data=$categoryModel->select();
        $data=getCatTree($data);
        //dump($data);die;
        $this->assign('data',$data);
        //系别分类修改
    	if(IS_POST){
        	if($categoryModel->create()){
        		if($categoryModel->save()!==false){
        			$this->success("修改成功",U("Category/index"));die;
        		}else{
        			$this->error("修改失败");
        		}
        	}else{
        		$this->error($categoryModel->getError());
        	}
        }
     	//系别分类回显
     	$cat_id=I('get.cat_id');
     	$cats=$categoryModel->find($cat_id);
     	//dump($cats);die;
        $this->assign('cats',$cats);
        $this->display();    	
    }
    //系别分类删除
    public function del(){
    	//实例化类模型
    	$categoryModel=D('Category');
    	$cat_id=I('get.cat_id');
    	$result=$categoryModel->where("pid=".$cat_id)->find();
    	if($result){
    		$this->error("有子分类不能删除");
    	}else{
    		$data=$categoryModel->delete($cat_id);
	    	if($data){
	    		$this->success("删除成功",U("Category/index"));die;
	    	}else{
	    		$this->error("删除失败");
	    	}
	    }
    }
}